from django.db import models
from datetime import datetime, date


class BookType(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=20,verbose_name='图书类型',default='')
    parent = models.ForeignKey("self",null=True,blank=True,verbose_name='父类',on_delete=models.DO_NOTHING,related_name='sub_book')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '图书类型'
        verbose_name_plural = '图书类型'
        db_table = 'book_type'

class BookInfo(models.Model):
    STATUS=(
        (0,'正常'),
        (1,'下架'),
    )
    id = models.AutoField(primary_key=True)
    type = models.ForeignKey(BookType,null=True,blank=True,verbose_name='图书类型',on_delete=models.DO_NOTHING)
    name = models.CharField(max_length=100,verbose_name='图书名称',default='')
    author = models.CharField(max_length=20,verbose_name='作者姓名',default='')
    picture = models.ImageField(verbose_name='图书主图', blank=True, null=True, upload_to='books/images/')
    page = models.IntegerField(default=0,verbose_name='图书页数')
    press = models.CharField(max_length=100,verbose_name='出版社',default='')
    price = models.DecimalField(max_digits=8, decimal_places=2,default=0,null=True,blank=True,verbose_name='标准价格')
    true_price = models.DecimalField(max_digits=8, decimal_places=2,default=0,null=True,blank=True,verbose_name='实际价格')
    status = models.IntegerField(default=0, choices=STATUS)
    publish_date = models.DateField(default=date.today, verbose_name='出版时间')
    purchase_date = models.DateField(default=date.today, verbose_name='购买时间')
    storage_location = models.CharField(max_length=100,null=True,blank=True,verbose_name='存储位置',default='')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '图书信息'
        verbose_name_plural = '图书信息'
        db_table = 'book_info'


